/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import DAO.DAOEmpresa;
import DAO.DAOFactory;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import modelo.Empresa;

/**
 *
 * @author JGS
 */
public class Buscar_Empresas extends javax.swing.JInternalFrame {

      private static Buscar_Empresas instancia = null;

    public static Buscar_Empresas getInstancia() {
        if (instancia == null) {
            instancia = new Buscar_Empresas();
        } else {
            instancia.dispose();
            instancia = new Buscar_Empresas();
        }
        return instancia;
    }
    private SingletonEmpresas empresas;
    
  
    public Buscar_Empresas() {
        initComponents();
        
                empresas = SingletonEmpresas.getInstancia();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        modificar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaEmpresas = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        cerrar = new javax.swing.JButton();
        buscar = new javax.swing.JButton();
        nuevo = new javax.swing.JButton();
        razonSocial = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        modificar.setText("Modificar");
        modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarActionPerformed(evt);
            }
        });

        tablaEmpresas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tablaEmpresas);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel3.setForeground(new java.awt.Color(153, 153, 153));
        jLabel3.setText("Empresas");

        jLabel1.setText("Ingrese Razón Social");

        cerrar.setText("Cerrar");
        cerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cerrarActionPerformed(evt);
            }
        });

        buscar.setText("Buscar");
        buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarActionPerformed(evt);
            }
        });

        nuevo.setText("Nuevo");
        nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nuevoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(560, 560, 560)
                .addComponent(nuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(modificar)
                .addGap(87, 87, 87)
                .addComponent(cerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(13, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 871, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(8, 8, 8)
                                        .addComponent(jLabel1)
                                        .addGap(18, 18, 18)
                                        .addComponent(razonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addComponent(buscar))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 871, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(10, 10, 10))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cerrar, modificar, nuevo});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(buscar)
                    .addComponent(razonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(modificar)
                    .addComponent(nuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cerrar))
                .addGap(81, 81, 81))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {cerrar, modificar, nuevo});

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevoActionPerformed
// TODO add your handling code here:
      Principal princ = Principal.getInstancia();
    Alta_Empresas alta = new Alta_Empresas();
    princ.desktop.add(alta);
    alta.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension ventana = alta.getSize();
    alta.setLocation((pantalla.width - ventana.width) / 2, (pantalla.height - ventana.height) / 2);
    alta.setVisible(true);
    this.dispose();
}//GEN-LAST:event_nuevoActionPerformed

private void modificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_modificarActionPerformed

private void buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarActionPerformed
// TODO add your handling code here:
    if (razonSocial.getText().length() == 0) JOptionPane.showMessageDialog(this,
                "Ingrese una razón social");
        else buscar();
    
}//GEN-LAST:event_buscarActionPerformed

private void cerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cerrarActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_cerrarActionPerformed

    /**
     * @param args the command line arguments
     */
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscar;
    private javax.swing.JButton cerrar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton modificar;
    private javax.swing.JButton nuevo;
    private javax.swing.JTextField razonSocial;
    private javax.swing.JTable tablaEmpresas;
    // End of variables declaration//GEN-END:variables

    private void buscar() {
        DAOEmpresa dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE).getEmpresaDAO();
        empresas.setEmpresa((LinkedList) dao.buscarPorRazon(razonSocial.getText()));
        ModeloTabla modelo = new ModeloTabla();
        Object tit[] = {"Razón Social", "Telefono","Contacto", "E-mail"};
        Object vals[][] = new Object[empresas.getEmpresa().size()][tit.length];
        for (int i = 0; i < empresas.getEmpresa().size(); i++) {
            Empresa aux = empresas.getEmpresa().get(i);
            Object datos[] = new Object[tit.length];
            datos[0] = aux.getRazonSocial();
            datos[1] = aux.getTelefono();
            datos[2] = aux.getContacto();
            datos[3] = aux.getEmail();
         
            vals[i] = datos;
        }
        modelo.setDataVector(vals, tit);
        this.tablaEmpresas.setModel(modelo);
        if (empresas.getEmpresa().size() == 0) {
            JOptionPane.showMessageDialog(this, "No se han encontrado pacientes");
        }
        
        
        
    }
}
